import { createSlice } from '@reduxjs/toolkit'

export const userSlice = createSlice({
  name: 'user',
  initialState: [
    {
        id:1,
        name:'陈不住气',
        phone:'19233167627',
        cardid:'13102220040526005X',
        status:true
    },
    {
        id:2,
        name:'章若楠',
        phone:'18513531931',
        cardid:'73127772363848966X',
        status:false
    },
  ],
  reducers: {
    changeStatus(state,{payload}){
        state[payload.index].status = payload.checked
    },
    delUser(state,{payload}){
        state.splice(payload,1)
    },
    addUser(state,{payload}){
        state.push({...payload,id:Math.random()*1000,status:false})
    },
    editUser(state,{payload}){
        state.forEach(item=>{
            if(item.id===payload.id){
                item.name=payload.name
                item.phone=payload.phone
                item.cardid=payload.cardid
            }
        })
    }
  },
})

// Action creators are generated for each case reducer function
export const { changeStatus,delUser,addUser,editUser } = userSlice.actions

export default userSlice.reducer